package com.example.demo_music;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.MediaStore;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MyService extends Service{
    private MediaPlayer mp;
    private List<Music> musicList;
    private int curPos = 0;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void onCreate(){
        musicList = getMusic();
        mp = new MediaPlayer();
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                next();
            }
        });
    }
        private void play(int position){
            try{
                mp.reset();
                mp.setDataSource(musicList.get(position).getData());
                mp.prepare();
                mp.start();
                curPos = position;
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        public void next(){
            ++curPos;
            if(curPos == musicList.size())
                curPos = 0;
            play(curPos);
        }
        public void previous(){
            --curPos;
            if(curPos == -1)
                curPos = musicList.size() - 1;
            play(curPos);
        }
        public List<Music> getMusic(){
            List<Music> musics = new ArrayList<>();
            String[] project = {
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.DATA
            };
            Cursor cursor = getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    project,
                    null,
                    null,
                    MediaStore.Audio.Media.TITLE
            );
            while(cursor.moveToNext()){
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = cursor.getString(1);
                long duration = cursor.getLong(2);
                String data = cursor.getString(3);
//                System.out.println(title + "," + artist + "," + duration + "," + data);
                musics.add(new Music(title,artist,duration,data));
            }
            return musics;
        }
        public int onStartCommand(Intent intent, int flags, int startId){
            String action = intent.getStringExtra("action");
            switch (action){
                case "play" :
                    int pos = intent.getIntExtra("pos", 0);
                    play(pos);
                    break;
                case "previous" :
                    previous();
                    break;
                case "next":
                    next();
                    break;
                case "start" :
                    mp.start();
                    break;
                case "pause" :
                    mp.pause();
                    break;
            }
            return super.onStartCommand(intent, flags, startId);
        }

}